//
//  ALAllShopsController.m
//  Ummy.G
//
//  Created by 安林 on 16/10/10.
//  Copyright © 2016年 安林. All rights reserved.
//

#import "ALAllShopsController.h"
#import "ALShopsTableViewCell.h"
#import <AFNetworking.h>
#import <MJRefresh.h>


@interface ALAllShopsController ()<UITableViewDataSource,UITableViewDelegate>
//存放模型数组的数组
@property (nonatomic,strong) NSMutableArray * arrM;
//添加那一堆自定义图片的数组
@property(nonatomic,strong)NSArray *ImgArr;


@end

@implementation ALAllShopsController


#pragma mark 模型数组的懒加载
-(NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM = [NSMutableArray array];
    }
    return _arrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self addLoad];
    
    //刷新数据
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(addLoad)];
    
    //进来就刷新
    [self.tableView.mj_header beginRefreshing];
    
    //由于没有抓到数据，只能自己手动添加图片啦
    _ImgArr = @[@"屏幕快照 2016-10-06 18.04.34",@"屏幕快照 2016-10-06 18.03.03",@"屏幕快照 2016-10-06 18.03.40",@"屏幕快照 2016-10-06 18.01.36",@"屏幕快照 2016-10-06 18.06.00",@"屏幕快照 2016-10-06 18.10.04",@"屏幕快照 2016-10-06 18.10.51",@"屏幕快照 2016-10-06 18.04.19",@"屏幕快照 2016-10-06 18.04.08",@"屏幕快照 2016-10-06 18.10.37",@"屏幕快照 2016-10-06 18.10.44",@"屏幕快照 2016-10-06 18.06.14",@"屏幕快照 2016-10-06 18.09.21",@"屏幕快照 2016-10-06 18.09.54",@"屏幕快照 2016-10-06 18.07.00",@"屏幕快照 2016-10-06 18.03.28",@"屏幕快照 2016-10-06 18.07.09",@"屏幕快照 2016-10-06 18.10.12",@"屏幕快照 2016-10-06 18.05.54",@"屏幕快照 2016-10-06 18.03.12"];

    
    //实现代理协议
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    //建立tableView
//    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 104, UIScreenW, UIScreenH)];
    
    
    //设置tableView内边距
    self.tableView.contentInset= UIEdgeInsetsMake(0, 0, 150, 0);
    
    //添加到view上
//    [self.view addSubview:self.tableView];
    
}

#pragma mark 返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrM.count;
}



#pragma mark 返回模型里的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID =@"Shops";
    ALShopDown *model = self.arrM[indexPath.row];
    ALShopsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [ALShopsTableViewCell loadALShopsTableViewCell];
    }
    
    
    //设置 tableViewCell 点击的状态为无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model =model;
    //把自己定义的照片数组添加上去
    cell.Img.image=[UIImage imageNamed:self.ImgArr[indexPath.row]];
    return cell;
}


#pragma mark 设置 tableViewCell 的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}



#pragma mark 加载“商家”界面的tableView的数据
- (void)addLoad{
    
    AFHTTPSessionManager *manger = [[AFHTTPSessionManager alloc]init];
    NSString *url = @"http://api.meituan.com/group/v1/poi/select/cate/-1?cityId=76&areaId=5537&mypos=38.041775%2C114.477095&sort=smart&coupon=hasgroup%7Cchoosesitting&offset=0&limit=20&fields=phone%2CscoreSource%2CmarkNumbers%2CcityId%2Caddr%2Clng%2ChasGroup%2CsubwayStationId%2Ccates%2CfrontImg%2CchooseSitting%2Cwifi%2CavgPrice%2Cstyle%2CfeatureMenus%2CavgScore%2Cname%2CparkingInfo%2Clat%2CcateId%2Cintroduction%2CshowType%2CareaId%2CdistrictId%2Cpreferent%2ClowestPrice%2CcateName%2CareaName%2CzlSourceType%2CcampaignTag%2CmallName%2CmallId%2CbrandId%2CbrandName%2CbrandLogo%2CbrandStory%2CsmPromotion%2Cktv%2Cgeo%2ChistoryCouponCount%2Crecommendation%2CiUrl%2CisQueuing%2CnewPayInfo%2CpayInfo%2CsourceType%2Cabstracts%2CgroupInfo%2CisSuperVoucher%2Cdiscount%2CisWaimai%2CcollectionDeals%2CnearPoi%2Csparkle%2CtopicList%2CcityIds%2CshowChannel%2CshowStatus&__vhost=api.mobile.meituan.com&utm_source=huawei&utm_medium=android&utm_term=431&version_name=7.3.1&utm_content=a0000055de24e8&utm_campaign=AgroupBgroupC0D200E0Gmerchant&ci=76&msid=a0000055de24e81475732261462&uuid=FDC1C5960287691C1AA0FDB6CE8736766612182AEF7A556CA2D3DB97774FE2E8&userid=180479389&__reqTraceID=8341cd3f-9487-4fb9-b3f2-8fed5c599958&__skck=6a375bce8c66a0dc293860dfa83833ef&__skts=1475732502347&__skua=58810434ff9b9a0bc6d4edd46aea3499&__skno=acad96a2-2000-4580-b302-beef6ddc8cf2&__skcy=5%2BvnbUpNi6i2hRcJDOOvyz8YO7I%3D";
    [manger GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //写成桌面plist文件
        //[responseObject writeToFile:@"/Users/anlin/Desktop/ShopDown.plist" atomically:YES];
        
        //请求数据
        NSArray *array = responseObject[@"data"];
        for (NSDictionary *dict in array) {
            ALShopDown *model = [ALShopDown shopmodelWithDict:dict];
            [self.arrM addObject:model];
        }
        //NSLog(@"12313%@",self.arrM);
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}


@end
